import com.lai.dao.RoleDao;
import com.lai.dao.UserDao;
import com.lai.entities.Role;
import com.lai.entities.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

/**
 * User: wlai
 * Date: 8/31/13
 * Time: 5:20 PM
 */
public class App {

    public static void main(String[] args) {
        ApplicationContext appContext =
                new ClassPathXmlApplicationContext("applicationContext*.xml ");

        UserDao dao = (UserDao) appContext.getBean("userDao");
        RoleDao roleDao=(RoleDao)appContext.getBean("roleDao");
        /*User user = new User();
        user.setName("lai");
        user.setPassword("123456");
        dao.addUser(user);
        List<User> users = dao.listUsers();
        for (User u : users) {
            System.out.println(u.getName());
        }
          */
        User user=dao.getUserByUsername("lai");
        Role role=roleDao.getRoleByRolename("ROLE_ADMIN");
        List<Role> roles=new ArrayList<Role>();
        roles.add(role);
        user.setRoles(roles);
        dao.updateUser(user);

       /* Role role=new Role();
        role.setName("ROLE_ADMIN");

        roleDao.addRole(role);*/

        System.out.println("Done");
    }
}
